package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.MessageBean;
import com.sanjiahantang.youjiarole.module.common.beans.MessageUseNoticeBean;
import com.sanjiahantang.youjiarole.module.common.models.IUserModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiarole.module.common.presenters.MessagePresenter;
import com.sanjiahantang.youjiarole.module.common.views.MessageView;
import com.sanjiahantang.youjiarole.module.decorate.views.impl.DecorateDetailActivity;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsDetailActivity;
import com.sanjiahantang.youjiarole.module.match.views.impl.MatchActivity;
import com.sanjiahantang.youjiarole.module.me.require.views.impl.RequireDetailActivity;
import com.sanjiahantang.youjiarole.module.me.scheme.views.impl.MeSchemeCaseDetailActivity;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by sks on 2015/9/23.
 * 我的消息
 */
public class MessageActivity extends BaseMvpActivity<MessageView, MessagePresenter> implements View.OnClickListener, MessageView,
        AdapterView.OnItemClickListener {
    private final int INTENT_REQUEST_CODE = 200;
    private Context context;
    private TextView title;
    private ImageView back;
    private ListView listView;
    private IUserModel userModel;
    private int index;
    private ArrayList<MessageBean.Data> list = new ArrayList<>();

    @Override
    public MessagePresenter initPresenter() {
        userModel = new UserModel();
        return new MessagePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_message);
        context = this;
        title = (TextView) findViewById(R.id.title);
        back = (ImageView) findViewById(R.id.back);
        back.setBackgroundResource(R.drawable.i_back_black);
        title.setText("通知中心");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);


        listView.setAdapter(presenter.getAdapter(context));
        listView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        presenter.getMessage(context);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("item", i + "//");
        index = i;
        final MessageBean.Data item = presenter.getDataList().get(index);
        if (item == null) {
            Utils.showToast(context, "无效通知");
            return;
        }
        if ("1".equals(presenter.getDataList().get(index).getReadFlg())) {
            //已读消息直接进入WebActivity
            if ("0".equals(item.getPushType())) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra(KEY_TYPE, WEB_TYPE_MESSAGE);
                intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getNoticeContent());
                startActivityForResult(intent, INTENT_REQUEST_CODE);
            } else if (("13".equals(item.getPushType()))) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra(KEY_TYPE, WEB_TYPE_RICHTEXT);
                intent.putExtra(KEY_ID, presenter.getDataList().get(index).getSnId());
                intent.putExtra(KEY_TITLE, presenter.getDataList().get(index).getNoticeTitle());
                startActivityForResult(intent, INTENT_REQUEST_CODE);
            } else {
                if (item.getPushDestId() == null) {
                    Utils.showToast(context, "无效通知");
                    return;
                }
                intentPage(item.getPushType(), item.getPushDestId());
            }
        } else {
            //未读消息访问已读未读接口后进入WebActivity
            Map<String,Object> params=new HashMap<>();
            params.put("snId",presenter.getDataList().get(i).getSnId());
            params.put("rolePhone", SPUtils.get(context, SPConfig.PHONE,"").toString());
            params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            userModel.getUseNoticeForCustomer(context,params,new CallBack<MessageUseNoticeBean>(){
                @Override
                public void onSuccess(MessageUseNoticeBean response, Context context, Object tag) {
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                        Log.e("返回", "" + response.getData());
                        initData();
                        if ("0".equals(item.getPushType())) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra(KEY_TYPE, WEB_TYPE_MESSAGE);
                            intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getNoticeContent());
                            startActivityForResult(intent, INTENT_REQUEST_CODE);
                        } else if ("13".equals(item.getPushType())) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra(KEY_TYPE, WEB_TYPE_RICHTEXT);
                            intent.putExtra(KEY_ID, presenter.getDataList().get(index).getSnId());
                            intent.putExtra(KEY_TITLE, presenter.getDataList().get(index).getNoticeTitle());
                            startActivityForResult(intent, INTENT_REQUEST_CODE);
                        } else {
                            if (item.getPushDestId() == null) {
                                Utils.showToast(context, "无效通知");
                                return;
                            }
                            intentPage(item.getPushType(), item.getPushDestId());
                        }

                    } else {
                        Utils.showToast(context, "无效通知");
                    }

                }

                @Override
                public void onFailure(String message) {
                    Utils.showToast(context, "无效通知");
                }
            });
        }
    }

    private void intentPage(String type, String pid) {
        if ("0".equals(type) || "13".equals(type)) {
            //打开首页
            Intent i = new Intent(context, WebActivity.class);
            if ("0".equals(type)) {
                i.putExtra("url", pid);
            } else {
                i.putExtra(KEY_ID, pid);
                i.putExtra(KEY_TYPE, WEB_TYPE_RICHTEXT);
            }
            context.startActivity(i);
        } else if ("1".equals(type) || "12".equals(type)) {
            //这里好像是接单 统一跳到接单页
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra(KEY_PAGE, 2);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("2".equals(type)) {
            //打开方案页面
            Intent i = new Intent(context, MeSchemeCaseDetailActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("3".equals(type)) {
            //获取表单详情页面数据
            Intent i = new Intent(context, GoodsDetailActivity.class);
            i.putExtra("goodsId", Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("4".equals(type)) {
            //获取表单详情页面数据
            Intent i = new Intent(context, MatchActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("6".equals(type)) {
            //获取表单详情页面数据
            Intent i = new Intent(context, DecorateDetailActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            i.putExtra(KEY_ID, pid);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("7".equals(type) || "9".equals(type) || "11".equals(type)) {
            Intent i = new Intent(context, RequireDetailActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra("state", 2);
            i.putExtra("type", 0);
            context.startActivity(i);
        } else if ("8".equals(type)) {
            Intent i = new Intent(context, RequireDetailActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra("state", 2);
            i.putExtra("type", 1);
            context.startActivity(i);
        } else if ("14".equals(type)){
            //进入该施工的装修管理页面
            Intent i = new Intent(context, DecorateDetailActivity.class);
            i.putExtra(KEY_ID, pid);
            context.startActivity(i);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_REQUEST_CODE) {
            if (!"1".equals(presenter.getDataList().get(index).getReadFlg())) {
                //点击未读消息进入WebActivity后，返回重新刷新消息列表；反之，不刷新
                presenter.getMessage(context);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }

    @Override
    public void showProgress() {

    }
}
